package com.minio.template;


import com.minio.property.MinioConfigurationProperties;
import io.minio.*;
import io.minio.errors.ErrorResponseException;
import io.minio.http.Method;
import io.minio.messages.Item;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: MinioTemplate
 * @Description: Minio动作模板实现
 * @Author: xuweiLi
 * @Create: 2025/7/4 20:23
 **/
public class MinioTemplate extends MinioAccessor implements MinioOperations{

    public MinioTemplate() {

    }

    @Override
    public void uploadObject(String objectName, InputStream stream,
                             long size, String contentType) throws Exception {
        MinioConfigurationProperties properties = this.getProperties();
        this.getMinioClient().putObject(PutObjectArgs.builder()
                .bucket(properties.getBucket())
                .object(objectName)
                .stream(stream,size,-1)
                .contentType(contentType)
                .build());
    }

    @Override
    public InputStream downloadObject(String objectName) throws Exception {
        MinioConfigurationProperties properties = this.getProperties();
        return this.getMinioClient().getObject(GetObjectArgs.builder()
                .bucket(properties.getBucket())
                .object(objectName)
                .build());
    }

    @Override
    public void deleteObject(String objectName) throws Exception {
        MinioConfigurationProperties properties = this.getProperties();
        this.getMinioClient().removeObject(RemoveObjectArgs.builder()
                .bucket(properties.getBucket())
                .object(objectName)
                .build());
    }

    @Override
    public String getObjectUrl(String objectName) throws Exception {
        MinioConfigurationProperties properties = this.getProperties();
        return String.join("/",properties.getEndpoint(),properties.getBucket(),objectName);
    }

    @Override
    public String getPresignedPutUrl(String objectName, int expiryMinutes) throws Exception {
        MinioConfigurationProperties properties = this.getProperties();
        return this.getMinioClient().getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.PUT)
                        .bucket(properties.getBucket())
                        .object(objectName)
                        .expiry(expiryMinutes, TimeUnit.MINUTES)
                        .build()
        );
    }

    @Override
    public List<Item> listObjects(String prefix) throws Exception {
        MinioConfigurationProperties properties = this.getProperties();
        List<Item> objects = new ArrayList<>();
        for (Result<Item> result : this.getMinioClient().listObjects(
                ListObjectsArgs.builder()
                        .bucket(properties.getBucket())
                        .prefix(prefix)
                        .recursive(true)
                        .build())) {
            objects.add(result.get());
        }
        return objects;
    }

    @Override
    public boolean objectExists(String objectName) throws Exception {
        MinioConfigurationProperties properties = this.getProperties();
        try {
            this.getMinioClient().statObject(StatObjectArgs.builder()
                    .bucket(properties.getBucket())
                    .object(objectName)
                    .build());
            return true;
        } catch (ErrorResponseException e) {
            if (e.errorResponse().code().equals("NoSuchKey")) {
                return false;
            }
            throw e;
        }
    }
}